package com.nondo.rdp.finance.endpoint.api.app;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.rdp.auth.common.UserUtils;
import com.nondo.rdp.auth.domain.Message;
import com.nondo.rdp.auth.domain.User;
import com.nondo.rdp.auth.domain.UserMessage;
import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.auth.endpoint.model.Query;
import com.nondo.rdp.auth.service.MessageService;
import com.nondo.rdp.auth.service.UserMessageService;
import com.nondo.rdp.auth.service.UserService;
import com.nondo.rdp.core.model.View;
import com.nondo.rdp.finance.domain.Setting;
import com.nondo.rdp.finance.service.SettingService;

@RestController("apiAppMessage")
@RequestMapping("/api/app/message")
public class MessageEndpoint {
	
	@Autowired
	SettingService settingService;
	
	@Autowired
	MessageService messageService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	UserMessageService userMessageService;

	@RequestMapping("/info/{id}")
	public View<Message> info(HttpServletRequest request, HttpServletResponse response,
			@PathVariable("id") Long id) {
		View<Message> view = new View<Message>(20000, "获取数据成功");
		
		Message message = null;
		if (id != null) {
			User user = UserUtils.getUser(request, response);
			UserMessage userMessage = userMessageService.findOne(id);
			if (!user.getId().equals(userMessage.getUser().getId())) {
				return View.ofError("对不起，你没有查看权限");
			}
			message = userMessage.getMessage();
			this.userMessageService.update(message.getId(), user.getId());
		} else {
			message = new Message();
		}

		view.setData(message);
		
		return view;
	}

	@RequestMapping("/list")
	public View<Page> list(HttpServletRequest request,
						   HttpServletResponse response, @RequestBody Query<UserMessage> query) {
		View<Page> view = new View(20000, "获取列表成功");

		UserMessage userMessage = query.getData();
		if(userMessage == null){
			userMessage = new UserMessage();
		}
		
		User user = UserUtils.getUser(request, response);
		userMessage.setUser(user);
		
		Page<UserMessage> page = userMessageService.findAll(
				new Page<UserMessage>(query.getPage(), query.getSize()), userMessage);

		List<UserMessage> userMessageList = page.getContent();
		for (UserMessage um : userMessageList) {
			Message message = um.getMessage();
			Setting setting = settingService.findByName("msgType_" + message.getType());
			if (setting != null) {
				message.setIcon(setting.getValue());
			} else {
				message.setIcon("");
			}
		}
		
		view.setData(page);

		return view;
	}
	
	@RequestMapping("/unReadNum")
	public View unReadNum(HttpServletRequest request, HttpServletResponse response) {
		View view = new View(20000, "获取数据成功");
		
		Message message = null;
		User user = UserUtils.getUser(request, response);
		Map<String, Object> reulstMap = new HashMap<String, Object>();
		int unReadNum = this.userMessageService.getUnReadNumByUserId(user.getId());
		reulstMap.put("unReadNum", unReadNum);
		view.setData(reulstMap);
		
		return view;
	}
	
	@RequestMapping("/markedAsRead/{idStr}")
	public View markedAsRead(HttpServletRequest request,
			HttpServletResponse response, @PathVariable("idStr") String idStr) {
		View view = new View(20000, "标记成功");

		if (StringUtils.isBlank(idStr)) {
			return View.ofError("必须至少指定一个要标记对象的ID");
		}
		Message message = null;
		UserMessage userMessage = null;
		User user = UserUtils.getUser(request, response);
		String[] ids = idStr.split(",");
		for (String id : ids) {
			userMessage = userMessageService.findOne(Long.parseLong(id));
			message = userMessage.getMessage();
			this.userMessageService.update(message.getId(), user.getId());
		}

		return view;
	}
	
	@RequestMapping("/delete/{idStr}")
	public View delete(HttpServletRequest request,
			HttpServletResponse response, @PathVariable("idStr") String idStr) {
		View view = new View(20000, "删除成功");
		if (StringUtils.isBlank(idStr)) {
			return View.ofError("必须至少指定一个要删除对象的ID");
		}
		UserMessage userMessage = null;
		User user = UserUtils.getUser(request, response);
		String[] ids = idStr.split(",");
		for (String id : ids) {
			userMessage = userMessageService.findOne(Long.parseLong(id));
			if (!user.getId().equals(userMessage.getUser().getId())) {
				return View.ofError("对不起，你没有删除权限");
			}
			this.userMessageService.delete(Long.parseLong(id));
		}

		return view;
	}
	
}